/*
更新日期：2017、01、16
作者：盛彬
备注信息：
日志 可扩充多种日志记录方式
公共模块，通过继承实现对派生类的基础功能补充
*/
#pragma once
#include "AllHeader.hpp"

#ifndef libLog_H 
#define libLog_H
class libLog    //log 
{
public:
    virtual int WriteLog(string _s_log);
    virtual int WriteLog(int _i_log);//在出问题位置打印唯一的标签
    virtual int WriteLog(string _s_log, int _i_log);//在出问题位置打印唯一的标签


    virtual int PrintLog(string _s_log); //测试用
    virtual int PrintLog(int _i_log);
    virtual int PrintLog(string _s_log, int _i_log);


    virtual int WriteTXT(string _filename, string _text);
    virtual int ProgramCommunication(u_short _ipport, string _text);//不适用 暂不使用吧
private:
    //    mutex mux_log; //日志锁  模块独立，加不加锁无所谓
};

inline int libLog::WriteLog(string _s_log)
{
//    mux_log.lock();
    string s_logname;

    time_t t_local = time(NULL);
    struct tm *_tm = localtime(&t_local);

    std::stringstream ss;
    ss << 1900 + _tm->tm_year << "_" << 1 + _tm->tm_mon << "_" << _tm->tm_mday << ".log ";
    ss >> s_logname;
    ss.str("");
    ss.clear();

    std::ofstream fl(s_logname, ios::app);
    if (0 != _s_log.length())
    {
        fl << _tm->tm_hour << ":" << _tm->tm_min << ":" << _tm->tm_sec << " # " << _s_log << endl;
    }
    fl.close();

//    mux_log.unlock();
    return 0;
}

inline int libLog::WriteLog(int _i_log)
{
    string s_logname;

    time_t t_local = time(NULL);
    struct tm *_tm = localtime(&t_local);

    std::stringstream ss;
    ss << 1900 + _tm->tm_year << "_" << 1 + _tm->tm_mon << "_" << _tm->tm_mday << ".log ";
    ss >> s_logname;
    ss.str("");
    ss.clear();


    std::ofstream fl(s_logname, ios::app);
    fl << _tm->tm_hour << ":" << _tm->tm_min << ":" << _tm->tm_sec << " # " << _i_log << endl;
    fl.close();

    return 0;
}

inline int libLog::WriteLog(string _s_log, int _i_log)
{
    string s_logname;

    time_t t_local = time(NULL);
    struct tm *_tm = localtime(&t_local);

    std::stringstream ss;
    ss << 1900 + _tm->tm_year << "_" << 1 + _tm->tm_mon << "_" << _tm->tm_mday << ".log ";
    ss >> s_logname;
    ss.str("");
    ss.clear();


    std::ofstream fl(s_logname, ios::app);
    fl << _tm->tm_hour << ":" << _tm->tm_min << ":" << _tm->tm_sec << " # " << _s_log << endl;
    fl << _tm->tm_hour << ":" << _tm->tm_min << ":" << _tm->tm_sec << " # " << _i_log << endl;
    fl.close();
    return 0;
}

inline int libLog::PrintLog(string _s_log)
{
    cout << _s_log << endl;
    WriteLog(_s_log);
    return 0;
}

inline int libLog::PrintLog(int _i_log)
{
    cout << _i_log << endl;
    WriteLog(_i_log);
    return 0;
}

inline int libLog::PrintLog(string _s_log, int _i_log)
{
    cout << _s_log << endl;
    cout << _i_log << endl;
    WriteLog(_s_log, _i_log);
    return 0;
}

inline int libLog::WriteTXT(string _filename, string _text)
{
    std::ofstream ofs(_filename, ios::out | ios::binary);
    if (ofs.is_open())
    {
        ofs << _text << endl;
        ofs.close();
    }
    return 0;
}

//一次性使用 注意清除残留
inline int libLog::ProgramCommunication(u_short _ipport, string _text)
{
    const int BUF_SIZE = 64;
    WSADATA wsd;
    SOCKET sHost;
    SOCKADDR_IN servAddr;
    char buf[BUF_SIZE];
    char bufRecv[BUF_SIZE];//none
    int retVal;

    if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
    {
        cout << "Failed in WSAStartup." << endl;
        return -1;
    }
    sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_GGP);
    if (INVALID_SOCKET == sHost)
    {
        cout << "Failed in WSAStartup." << endl;
        WSACleanup();
        return -1;
    }

    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//本地
    servAddr.sin_port = htons(_ipport);
    int nServAddlen = sizeof(servAddr);

    retVal = connect(sHost, (LPSOCKADDR)&servAddr, nServAddlen);
    if (INVALID_SOCKET == retVal)
    {
        cout << "Failed in connect." << endl;
        closesocket(sHost);
        WSACleanup();
        return -1;
    }
    ZeroMemory(buf, BUF_SIZE);
    cout << _text.c_str();
    std::cin >> buf; //注意内存变量
    retVal = send(sHost, buf, strlen(buf), 0);
    if (INVALID_SOCKET == retVal)
    {
        cout << "Failed in send." << endl;
        closesocket(sHost);
        WSACleanup();
        return -1;
    }

    closesocket(sHost);
    WSACleanup();
    return 0;
}

#endif //libLog_H